package com.admin.exam.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.admin.exam.entity.QuestionImport;
import com.admin.exam.service.ExcelListener;
import com.admin.exam.service.ExcelListener2;
import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.event.AnalysisEventListener;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

import com.admin.base.Page;
import com.admin.base.BaseController;
import com.admin.base.Response;
import com.admin.exam.entity.Question;
import com.admin.exam.service.QuestionService;
import com.admin.exam.vo.query.QuestionQuery;

import java.io.*;
import java.lang.reflect.InvocationTargetException;
import java.util.List;

/**
 *
 */
@Controller
@RequestMapping(value = "/exam/question")
public class QuestionController extends BaseController {

    @Autowired
    QuestionService questionService;

    /**
     * 列表页面
     */
    @RequestMapping(value = "/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("exam/question/list");
        return mav;
    }

    /**
     * 分页查询数据
     */
    @RequestMapping(value = "/paging")
    @ResponseBody
    public Response paging(HttpServletRequest request, HttpServletResponse response, QuestionQuery query) {
        Page<Question> page = questionService.pagingByConditions(query);
        return Response.ok(page);
    }

    /**
     * 新增页面
     */
    @RequestMapping(value = "/toAdd")
    public ModelAndView toAdd(HttpServletRequest request, HttpServletResponse response, ModelAndView mav) {
        mav.setViewName("exam/question/add");
        return mav;
    }

    /**
     * 保存数据
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Response add(HttpServletRequest request, HttpServletResponse response, Question question) {
        questionService.add(question);
        return Response.ok();
    }

    /**
     * 删除数据
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Response delete(HttpServletRequest request, HttpServletResponse response, Integer[] id) {
        questionService.delete(id);
        return Response.ok();
    }

    /**
     * 编辑页面
     */
    @RequestMapping(value = "/toEdit")
    public ModelAndView toEdit(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        Question question = questionService.getById(id);
        mav.addObject(question);
        mav.setViewName("exam/question/edit");
        return mav;
    }

    /**
     * 详情页面
     */
    @RequestMapping(value = "/detail")
    public ModelAndView detail(HttpServletRequest request, HttpServletResponse response, ModelAndView mav, Integer id) {
        Question question = questionService.getById(id);
        mav.addObject(question);
        mav.setViewName("exam/question/detail");
        return mav;
    }

    /**
     * 更新数据
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Response update(HttpServletRequest request, HttpServletResponse response, Question question) {
        questionService.update(question);
        return Response.ok();
    }

    @RequestMapping(value = "/importQuestion")
    @ResponseBody
    public Response importQuestion(MultipartFile file, HttpServletRequest request, HttpServletResponse response, ModelAndView mav) throws Exception {
        AnalysisEventListener listener = new ExcelListener(questionService);
        EasyExcel.read(multipartFileToFile(file), QuestionImport.class, listener).sheet().doRead();
        return Response.ok();
    }

    @RequestMapping(value = "/multiQuestion")
    @ResponseBody
    public Response multiQuestion(MultipartFile file, HttpServletRequest request, HttpServletResponse response, ModelAndView mav) throws Exception {
        AnalysisEventListener listener = new ExcelListener2(questionService);
        EasyExcel.read(multipartFileToFile(file), QuestionImport.class, listener).sheet().doRead();
        return Response.ok();
    }

    public static File multipartFileToFile(MultipartFile file) throws Exception {

        File toFile = null;
        if (file.equals("") || file.getSize() <= 0) {
            file = null;
        } else {
            InputStream ins = null;
            ins = file.getInputStream();
            toFile = new File(file.getOriginalFilename());
            inputStreamToFile(ins, toFile);
            ins.close();
        }
        return toFile;
    }

    //获取流文件
    public static void inputStreamToFile(InputStream ins, File file) {
        try {
            OutputStream os = new FileOutputStream(file);
            int bytesRead = 0;
            byte[] buffer = new byte[8192];
            while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
                os.write(buffer, 0, bytesRead);
            }
            os.close();
            ins.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
